package com.youbo.config;

/**
 * @author: macy
 * @Email: 420534221@qq.com
 * @Date: 2020/11/3
 * @describe:
 */

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class LogFilter {



    // 统一切点,对com.hangtian.admin.controller及其子包中所有的类的所有方法切面
    @Pointcut("execution(public * com.youbo.controller..*.*(..))")
    public void Pointcut() {
    }

    @Before("Pointcut()")
    public void before(JoinPoint joinPoint){
        Object args[] = joinPoint.getArgs();
        MethodSignature sig = (MethodSignature)joinPoint.getSignature();
        Method method = sig.getMethod();
        if(null != method.getDeclaringClass().getName() && null != method.getName() && null != args && args.length > 0){
            log.debug("{} . {} : 请求参数：{}",method.getDeclaringClass().getName(),method.getName(), StringUtils.join(args," : "));
        }
    }

    @AfterReturning(value="Pointcut()",returning="rvt")
    public void after(JoinPoint joinPoint,Object rvt){
        MethodSignature sig1 = (MethodSignature)joinPoint.getSignature();
        Method method1 = sig1.getMethod();
        if(null != rvt && null != method1.getDeclaringClass()){
            try{
                log.debug("{} . {} : 返回数据：{}",method1.getDeclaringClass().getName(),method1.getName(),new Gson().toJson(rvt));
            }catch (Exception e){

            }
        }
    }


}
